/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.images;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import java.util.UUID;

/**
 * <p>Project: jse2204 - CapchaDemo
 * <p>Powered by webrx On 2022-09-27 09:18:15
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class CapchaDemo {
    public static void main(String[] args) throws Exception {
        int len = 4;
        int width = len * 26;
        int height = 50;
        String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var rand = new Random();
        StringBuilder sbu = new StringBuilder();
        var cc = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
        var g = cc.createGraphics();
        setRenderingHint(g);

        g.setColor(new Color(rand.nextInt(26)+220,rand.nextInt(26)+220,rand.nextInt(26)+220));
        g.fillRect(0,0,width,height);


        var c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        g.setColor(c);
        for(int i=0;i<len;i++) {
            String str = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
            sbu.append(str);
            var x = i * 25 + 5;
            var y = 35;

            double radianPercent = Math.PI * (rand.nextInt(35) / 180D);
            if (rand.nextBoolean())
                radianPercent = -radianPercent;
            g.rotate(radianPercent, x, y);
            g.setFont(new Font("微软雅黑", Font.PLAIN, rand.nextInt(20) + 20));
            g.drawString(str, x, y);
            g.rotate(-radianPercent, x, y);
        }


        //干扰文件
        for(int i=0;i<len*10;i++) {
            var x = rand.nextInt(width);
            var y = rand.nextInt(height);
            String str = String.valueOf(letter.charAt(rand.nextInt(letter.length())));
            double radianPercent = Math.PI * (rand.nextInt(35) / 180D);
            //if (rand.nextBoolean())
            //    radianPercent = -radianPercent;
            //g.rotate(radianPercent, x, y);

            c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256),rand.nextInt(30)+60);
            g.setColor(c);

            g.setFont(new Font("微软雅黑", Font.PLAIN, rand.nextInt(10) + 10));
            g.drawString(str, x, y);
        }

        //干扰正统曲线
        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(100) + 50));

        int yy = rand.nextInt(35) + 15;// y
        int hh = rand.nextInt(20) + 10;// 高度
        int aa = rand.nextInt(60) + 20;//

        for (int x = 0; x < width; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            // g.drawLine(x, (int) y, x, (int) y);
            g.fillOval(x, y, 5, 5);
        }


        g.dispose();
        //ImageIO.write(cc,"png",new File(UUID.randomUUID().toString().concat(".png")));
        ImageIO.write(cc,"png",new File("cc.png"));
        Runtime.getRuntime().exec("cmd /k start cc.png");

    }

    /**
     * 设置字体 边线 平滑
     * @param g
     */
    public static void setRenderingHint(Graphics2D g){
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
    }

    public static Font getRandFont(int start,int end){
        Random rand = new Random();
        Font font  = null;
        if(rand.nextBoolean()){
            font = new Font("微软雅黑", Font.PLAIN, rand.nextInt(end) + start);
        }else{
            try{
                Font f = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/cs.ttf"));
                font = f.deriveFont(Font.PLAIN, rand.nextInt(end) + start);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return font;
    }
}
